package com.example.demo;

import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * @author lizhien (zhien.li@nascent.cn)
 * @version 1.0.0
 * @date 2022/3/18 16:02
 * @description com.example.demo
 */

@Component
@Slf4j
public class TimeFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) {
        log.info("过滤器===TimeFilter 初始化");
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        log.info("过滤器===TimeFilter doFilter开始");
        long start = System.currentTimeMillis();
        filterChain.doFilter(servletRequest, servletResponse);
        log.info("过滤器===TimeFilter 耗时 : " + (System.currentTimeMillis() - start) + "ms");
        log.info("过滤器===TimeFilter doFilter结束");
    }

    @Override
    public void destroy() {
        log.info("TimeFilter 销毁");
    }
}
